//source: http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html
package Andrew;

import java.io.File;
import java.util.Scanner;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLParser {

	public static void query(final String query) {
		try {
			final File file = new File("Andrew/MyXMLFile.xml");
			final DocumentBuilderFactory dbf = DocumentBuilderFactory
					.newInstance();
			final DocumentBuilder db = dbf.newDocumentBuilder();
			final Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			System.out.println("Root element "
					+ doc.getDocumentElement().getNodeName());
			final NodeList nodeLst = doc.getElementsByTagName(query
					.toLowerCase());
			System.out.println(query + ":");

			for (int s = 0; s < nodeLst.getLength(); s++) {

				final Node fstNode = nodeLst.item(s);

				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

					final Element fstElmnt = (Element) fstNode;
					final NodeList titleElmntLst = fstElmnt
							.getElementsByTagName("title");
					final Element titleElmnt = (Element) titleElmntLst.item(0);
					final NodeList title = titleElmnt.getChildNodes();
					System.out.println((title.item(0)).getNodeValue());
				}

			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(final String argv[]) {
		System.out.println("Type of content to browse? Movie | TVShows");
		final Scanner s = new Scanner(System.in);
		if (s.hasNext()) {
			XMLParser.query(s.nextLine());
			s.close();
		}

	}
}
